<form method="post" id="frmSearch" action="<?= base_url() ?>company/index/">
    <div style="text-align: center;vertical-align: middle;">
        <select name="ddPropinsi" id="ddPropinsi">
            <option value="">Select</option>
            <? foreach (getPropinsi() as $k => $v) { ?>
                <option value="<?= $v->province_id ?>"<?= @$ddPropinsi == $v->province_id ? ' selected="selected"' : "" ?>><?= $v->province_nama ?></option>
            <? } ?>
        </select>
        <select name="ddKota" id="ddKota">
            <option value="">Select</option>
            <? foreach (getKabupaten(@$ddPropinsi) as $k => $v) { ?>
                <option value="<?= $v->city_id ?>"<?= @$ddKota == $v->city_id ? ' selected="selected"' : "" ?>><?= $v->city_nama ?></option>
            <? } ?>
        </select>
        <input type="text" name="txtSearch" id="txtSearch" value="<?= @$txtSearch ?>"/>
        <input type="submit" class="btn-primary" value="Cari"/>
    </div>
</form>
<table class="table table-bordered table-striped responsive-utilities">
    <thead>
        <tr>
            <th>Nama</th>
            <th>Alamat</th>
            <th>Description</th>
            <th>Action</th>
        </tr>
    </thead>
    <tbody>
        <? if (@$dt): ?>
            <? foreach ($dt as $k => $v): ?>
                <tr>
                    <td><?= $v->company_name ?></td>
                    <td><?= $v->company_address ?>, <?= getNamaKota($v->city_id) ?>, <?= getNamaPropinsi($v->province_id) ?></td>
                    <td><?= $v->company_description ?></td>
                    <td>
                        <a href="<?= base_url() ?>company/edit/<?= $v->company_id ?>/">Edit</a>
                    </td>
                </tr>
            <? endforeach; ?>
            <? if ($this->paginationpopup->create_links() <> "") : ?>
                <tr>
                    <th colspan="4" style="text-align: center;"><?php echo $this->paginationpopup->create_links(); ?></th>
                </tr>
            <? endif; ?>
        <? else: ?>
            <tr>
                <td colspan="4">Data perusahaan tidak ditemukan.</td>
            </tr>
        <? endif; ?>
    </tbody>
</table>
<div class="modal hide fade" id="free"></div>
<input type="button" class="btn-primary" onclick="addCompany()" data-toggle="modal"  href="#free" value="Tambah Perusahaan"/>
<script>
    function pageClick(page){
        var action = $("#frmSearch").attr("action");
        $("#frmSearch").attr("action", action + page);
        $("#frmSearch").submit();
    };
    
    function addCompany(){
        $('#free').load("<?= base_url() ?>company/popupadd/admin");
    }
    
    function setCompany(id, name){
        alert('Perusahaan berhasil disimpan.');
        window.location = '<?= base_url() ?>company';
    }

    $('#ddPropinsi').change(function(){
        $('#ddKota').empty();
        $.post('<?= base_url() . "region/kabupaten" ?>', {id:$('#ddPropinsi').val()}, function(data) {
            if(data){
                var datajs = $.parseJSON(data);
                $('<option value="">Select</option>').appendTo("#ddKota");
                $.each(datajs, function(key, value){
                    $("<option value='"+value.city_id+"'>"+value.city_nama+"</option>").appendTo("#ddKota");
                });
            }
        });
    });
</script>
